package whf.framework.report;

import java.util.List;

import javax.servlet.ServletRequest;

import whf.framework.security.UserContext;
import whf.framework.util.ThreadContext;
import whf.framework.util.Utils;
import whf.framework.web.tag.TableColumn;
import whf.framework.web.tag.TableModel;
import whf.framework.web.tag.ext.IColumn;

/**
 * @author King
 * @create Jan 17, 2008 2:06:46 PM
 */
public class ReportUtils {
	private static ThreadLocal<ReportDescriptor> reportDescriptor = new ThreadLocal<ReportDescriptor>();
	
	/**
	 * 获取当前线程上下文的报表描述文件
	 * @return
	 */
	public static ReportDescriptor getReportDescriptor() {
		ReportDescriptor result = reportDescriptor.get();
		if(result != null && result.getUserContext() == null) {
			result.setUserContext(ThreadContext.getUserContext());
		}
		return result;
	}
	
	/**
	 * 设置报表描述上下文
	 * @param descriptor
	 */
	public static void setReportDescriptor(ReportDescriptor descriptor) {
		reportDescriptor.set(descriptor);
	}
	
	public static void setReportDescriptor(UserContext userContext, TableModel tableModel) {
		List<IColumn> reportColumns = Utils.newArrayList();
		for(TableColumn col: tableModel.getColumns()) {
			reportColumns.add(col);
		}
		ReportDescriptor descriptor = new ReportDescriptor(userContext, tableModel.getDataSource(), reportColumns, tableModel.getMeta());
		reportDescriptor.set(descriptor);
	}
	//
	public static boolean isExportMode(ServletRequest request) {
		return "true".equalsIgnoreCase(request.getParameter(ReportConstants.EXPORT));
	}
	
}
